Page({

  /**
   * 页面的初始数据
   */
  data: {
    plans: [
      [
        {
          date: "2018年10月15日",
          day: "星期一",
          type: "城区管网巡查",
          road: '西湖区龙井路',
          content: "盖周边有无污水冒出，有无清水进入管道",
          state: "已通过"
        },
        {
          date: "2018年10月16日",
          day: "星期二",
          type: "市政设施巡查",
          road: '杨公堤',
          content: "道路路面是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月17日",
          day: "星期三",
          type: "截污干管巡查",
          road: '西湖区南山路',
          content: "雨水井盖损坏或丢失",
          state: "已通过"
        },
        {
          date: "2018年10月18日",
          day: "星期四",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月19日",
          day: "星期五",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月20日",
          day: "星期六",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月21日",
          day: "星期日",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
      ],
      [
        {
          date: "2018年10月22日",
          day: "星期一",
          type: "城区管网巡查",
          road: '西湖区龙井路',
          content: "盖周边有无污水冒出，有无清水进入管道",
          state: "已通过"
        },
        {
          date: "2018年10月23日",
          day: "星期二",
          type: "市政设施巡查",
          road: '杨公堤',
          content: "道路路面是否损坏",
          state: "审批中"
        },
        {
          date: "2018年10月24日",
          day: "星期三",
          type: "截污干管巡查",
          road: '西湖区南山路',
          content: "雨水井盖损坏或丢失",
          state: "审批中"
        },
        {
          date: "2018年10月25日",
          day: "星期四",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "未提交"
        },
        {
          date: "2018年10月26日",
          day: "星期五",
        },
        {
          date: "2018年10月27日",
          day: "星期六",
        },
        {
          date: "2018年10月28日",
          day: "星期日",
        },
      ],
      [
        {
          date: "2018年10月29日",
          day: "星期一",
          type: "",
        },
        {
          date: "2018年10月30日",
          day: "星期二",
        },
        {
          date: "2018年10月31日",
          day: "星期三",
        },
        {
          date: "2018年11月01日",
          day: "星期四",
        },
        {
          date: "2018年11月02日",
          day: "星期五",
        },
        {
          date: "2018年11月03日",
          day: "星期六",
        },
        {
          date: "2018年11月04日",
          day: "星期日",
        },
      ]
    ],
    today: "",
    times: "",
    week: "",
    year: "",
    left: "<",
    right: ">",
    stateStill: '#ffffff',
    contentLeft: '',
    swiperCurrent: 1,
  },
  //swiper跳转下一页(切换日期)
  fnSwiperChange: function (e) {
    let num = this.num;
    let now = e.detail.current;
    this.setData({
      swiperCurrent:now,
    })
    let arrow;
    if (now == 0 && num == 2) {
      arrow = 1;
    }
    else if (now == 1 && num == 0) {
      arrow = 1;
    }
    else if (now == 2 && num == 1) {
      arrow = 1;
    }
    else {
      arrow = 0;
    }

    let plans = this.data.plans;
    let times = this.data.times;
    let next;
    // console.log(plans,times)
    if (arrow) {
      times += 24 * 3600 * 7 * 1000;
      next=now+1>2?0:now+1;
      plans[next]=this._getdata(times+24 * 3600 * 7 * 1000)
    }
    else {
      times -= 24 * 3600 * 7 * 1000;
      next=now-1<0?2:now-1;
      plans[next]=this._getdata(times-24 * 3600 * 7 * 1000)
    }
    this.setData({
      plans,
    })
    this._showTime(times);
    this.num = now;
  },
  //点击跳转下一页
  fnPlanTap: function (e) {
    wx.navigateTo({
      url: 'addplan/addplan?data=' + JSON.stringify(e.currentTarget.dataset.item) + '&plans=' + JSON.stringify(e.currentTarget.dataset.plans)+'&current='+e.currentTarget.dataset.id
    })
  },
  //左右按钮点击切换日期
  fnWeekTap: function (e) {
    let swiperCurrent=this.data.swiperCurrent;
    if (e.target.dataset.id == '1') {
      swiperCurrent=swiperCurrent+1>2?0:swiperCurrent+1;
    }
    else {
      swiperCurrent=swiperCurrent-1<0?2:swiperCurrent-1;
    }
    this.setData({
      swiperCurrent,
    })
  },

  //封装函数：显示week,year
  _showTime: function (times) {
    let week = this._dataList(times);
    let year = this._getDate(times, "year");
    this.setData({
      times,
      week,
      year
    })
  },
  //封装函数：通过传入时间戳，获取这个星期的日期的数据
  _getdata: function (times) {
    let week = this._dataList(times);
    //(这部分应为获取这个星期的日期的数据，实际是为假数据)
    let plan;
    if (week.indexOf(this.data.today) > -1) {
      plan = [
        {
          date: "2018年10月22日",
          day: "星期一",
          type: "城区管网巡查",
          road: '西湖区龙井路',
          content: "盖周边有无污水冒出，有无清水进入管道",
          state: "已通过"
        },
        {
          date: "2018年10月23日",
          day: "星期二",
          type: "市政设施巡查",
          road: '杨公堤',
          content: "道路路面是否损坏",
          state: "审批中"
        },
        {
          date: "2018年10月24日",
          day: "星期三",
          type: "截污干管巡查",
          road: '西湖区南山路',
          content: "雨水井盖损坏或丢失",
          state: "审批中"
        },
        {
          date: "2018年10月25日",
          day: "星期四",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "未提交"
        },
        {
          date: "2018年10月26日",
          day: "星期五",
          type: "",
          road: '',
          content: "",
          state: ""
        },
        {
          date: "2018年10月27日",
          day: "星期六",
          type: "",
          road: '',
          content: "",
          state: ""
        },
        {
          date: "2018年10月28日",
          day: "星期日",
          type: "",
          road: '',
          content: "",
          state: ""
        },
      ]
    }

    else if(week.indexOf(this.data.lastweek) > -1){
     plan=[
        {
          date: "2018年10月15日",
          day: "星期一",
          type: "城区管网巡查",
          road: '西湖区龙井路',
          content: "盖周边有无污水冒出，有无清水进入管道",
          state: "已通过"
        },
        {
          date: "2018年10月16日",
          day: "星期二",
          type: "市政设施巡查",
          road: '杨公堤',
          content: "道路路面是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月17日",
          day: "星期三",
          type: "截污干管巡查",
          road: '西湖区南山路',
          content: "雨水井盖损坏或丢失",
          state: "已通过"
        },
        {
          date: "2018年10月18日",
          day: "星期四",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月19日",
          day: "星期五",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月20日",
          day: "星期六",
          type: "市政设施巡查",
          road: '西湖区桃源岭',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
        {
          date: "2018年10月21日",
          day: "星期日",
          type: "市政设施巡查",
          road: '西湖区桃源岭，西湖区龙井路，杨公堤，西湖区南山路，西湖区龙井路',
          content: "人行道是否损坏、人行道围栏是否损坏",
          state: "已通过"
        },
      ];
    }
    else {
      plan = [
        {day:'星期一',type: "",road: '',content: "",state: ""},
        {day:'星期二',type: "",road: '',content: "",state: ""},
        {day:'星期三',type: "",road: '',content: "",state: ""},
        {day:'星期四',type: "",road: '',content: "",state: ""},
        {day:'星期五',type: "",road: '',content: "",state: ""},
        {day:'星期六',type: "",road: '',content: "",state: ""},
        {day:'星期日',type: "",road: '',content: "",state: ""},
      ];
      for(var i = 0; i < 7; i++){
        plan[i].date=week[i];
      }
    }
    
    return plan;
  },
  //封装函数：通过传入时间戳，将这个星期的日期的数据显示出来
  _showData: function (times) {
    let plans = this.data.plans;
    let week = this._dataList(times);
    let year = this._getDate(times, "year");

    for (var i = 0; i < 7; i++) {
      plans[i].date = week[i]
    };
    this.setData({
      times,
      week,
      plans,
      year
    })
  },
  //传入某一天的时间戳，得到这个星期的日期（从星期一到星期日）；
  _dataList: function (value) {
    let day = this._getDate(value, "day") - 1;
    let zero = value - 24 * 3600 * 1000 * day;
    let list = [];
    for (var i = 0; i < 7; i++) {
      list.push(this._getDate(zero + i * 24 * 3600 * 1000, "date"))
    }
    return list;
  },
  //时间戳转换为星期几/日期/年
  _getDate: function (value, type) {
    if (type == "day") {
      return new Date(parseInt(value)).getDay();
    }
    if (type == "date") {
      return this._toTwo(new Date(parseInt(value)).getFullYear()) + '年' + this._toTwo(new Date(parseInt(value)).getMonth() + 1) + '月' + this._toTwo(new Date(parseInt(value)).getDate()) + '日';
    }
    if (type == "year") {
      return new Date(parseInt(value)).getFullYear();
    }
    console.log('参数不正确')
  },
  //补零函数
  _toTwo: function (num) {
    if (num > 9) {
      return "" + num;
    }
    return "0" + num;
  },
  onLoad: function (options) {
    // console.log(this.data.plans[0][0])
    let time = new Date;
    let times = time.getTime();
    let year = time.getFullYear();
    let month = this._toTwo(time.getMonth() + 1);
    let date = this._toTwo(time.getDate());
    let day = this._toTwo(time.getDay());
    let today = year + '年' + month + '月' + date + '日';
    let week = this._dataList(times);
    this.data.lastweek=this._getDate(times-24 * 3600 * 1000 * 7,'date');
    this.num = 1;
    this.setData({
      today,
      times,
      week,
      year
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})